package com.ywy.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywy.entity.Category;
import com.ywy.service.CategoryService;
import com.ywy.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * 项目初始化
 *
 * @author ywy
 * @date 2021/12/24 15:23
 */
@Slf4j
@Component
public class ContextStartup implements ApplicationRunner, ServletContextAware {
    private ServletContext servletContext;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private PostService postService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 项目路径
        servletContext.setAttribute("basePath", servletContext.getContextPath());

        log.info("ContextStartup----->加载分类信息");
        List<Category> categories = categoryService.list(new QueryWrapper<Category>().eq("status", 1));
        // 当前选择的分类
        servletContext.setAttribute("categories", categories);

        log.info("ContextStartup----->加载本周热议");
        postService.initWeekRank();
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
